package com.axs.camera;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.axs.camera.adapter.PhotoAdapter;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;

import java.io.File;

import io.reactivex.Observable;

public class MainActivity extends AppCompatActivity {
    private RecyclerView rvPhotoList;
    private TextView tvInto;
    private TextView tvNum;
    private TextView tv_huajian;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rvPhotoList = findViewById(R.id.rv_photo_list);
        tvInto = findViewById(R.id.tv_into);
        tvNum = findViewById(R.id.tv_num);
        tv_huajian = findViewById(R.id.tv_huajian);
        tv_huajian.setOnClickListener(v -> Toast.makeText(MainActivity.this, "开发中......", Toast.LENGTH_SHORT).show());

        initAdapter();
    }

    @Override
    protected void onStart() {
        super.onStart();
        initAdapter();
    }

    private void initAdapter() {
        String photoPath = getExternalCacheDir().getAbsolutePath();
        File file = new File(photoPath);
        if (file.listFiles().length > 0) {
            tvInto.setVisibility(View.INVISIBLE);
            tvNum.setText(String.valueOf(file.listFiles().length));

            GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
            rvPhotoList.setLayoutManager(layoutManager);
            rvPhotoList.setAdapter(new PhotoAdapter(file.listFiles()));
        }
        for (File listFile : file.listFiles()) {
            String name = listFile.getName();
            Log.e("TAG", "onCreate: " + name);
        }
    }

    /**
     * 启动拍照界面
     */
    public void gotoCamare(View view) {
        PermissionUtils.applicationPermissions(MainActivity.this, new PermissionUtils.PermissionListener() {
            @Override
            public void onSuccess(Context context) {
                Intent intent = new Intent(MainActivity.this, MainActivity2.class);
                MainActivity.this.startActivity(intent);
            }

            @Override
            public void onFailed(Context context) {
                if (AndPermission.hasAlwaysDeniedPermission(context, Permission.Group.CAMERA)
                        && AndPermission.hasAlwaysDeniedPermission(context, Permission.Group.STORAGE)) {
                    AndPermission.with(context).runtime().setting().start();
                }
                Toast.makeText(context, context.getString(R.string.permission_camra_storage), Toast.LENGTH_SHORT);
            }
        }, Permission.Group.STORAGE, Permission.Group.CAMERA);
    }
}